﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.AubitFile;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using System.ComponentModel.DataAnnotations;

namespace FarmSmartSleep.Organ.API.Application.SleepInfoCommands.FemaleBeginCommands
{
    public class FemaleBeginSelectHandler : IRequestHandler<FemaleBeginSelectCommand, Result>
    {
        IBaseRepository<FemaleBegin> femalebeginRep;
        IBaseRepository<Cottages> cottagesRep;
        IBaseRepository<Fields> fieldsRep;
        IBaseRepository<SleepInfor> sleepRep;

        public FemaleBeginSelectHandler(IBaseRepository<FemaleBegin> femalebeginRep, IBaseRepository<Cottages> cottagesRep, IBaseRepository<Fields> fieldsRep, IBaseRepository<SleepInfor> sleepRep)
        {
            this.fieldsRep = fieldsRep;
            this.femalebeginRep = femalebeginRep;
            this.cottagesRep = cottagesRep;
            this.sleepRep = sleepRep;       
        }



        public async Task<Result> Handle(FemaleBeginSelectCommand request, CancellationToken cancellationToken)
        {
            var query = new Result() { Code = (int)CodeEnum.success };

            var list = (from a in femalebeginRep.GetAll()
                        join b in cottagesRep.GetAll() on a.CottagesId equals b.CottagesId
                        join c in fieldsRep.GetAll() on a.Field equals c.FieIdId
                        join d in sleepRep.GetAll() on a.SleepInforId equals d.SleepInforId
                        where a.Deletes == false
                        select new FemaleBeginDto()
                        {
                            FemaleBeginId = a.FemaleBeginId,
                            SleepInforId = a.SleepInforId,
                            CottageId = b.CottagesId,
                            CottageName = b.CottageName,
                            FieIdId = c.FieIdId,
                            FieIdName = c.FieIdName,
                            TransplainData = a.TransplainData,
                            FemaleBeginWeight = a.FemaleBeginWeight,
                            CreateId = a.CreateId,
                            CreateTime = a.CreateTime,
                            UpdateId = a.UpdateId,
                            UpdateTime = a.UpdateTime,
                            Deletes = a.Deletes,
                            DeleteId = a.DeleteId,
                            DeleteTime = a.DeleteTime,
                            StayStatus = d.StayStatus
                        });

            if (request.SleepInforId!=0)
            {
                list = list.Where(x => x.SleepInforId==request.SleepInforId);
            }

            if (!string.IsNullOrEmpty(request.start))
            {
                list = list.Where(x => x.TransplainData >= DateTime.Parse(request.start));
            }
            if (!string.IsNullOrEmpty(request.end))
            {
                list = list.Where(x => x.TransplainData < DateTime.Parse(request.end));
            }

            query.Code = (int)CodeEnum.success;
            query.Data = list;
            return query;
        }



        public class FemaleBeginDto:Aubuit
        {
            /// <summary>
            /// 种母期初编号
            /// </summary>
            public int FemaleBeginId { get; set; }//种母期初编号
            /// <summary>
            /// 耳号
            /// </summary>
            public int SleepInforId { get; set; }//耳号
            /// <summary>
            /// 栋舍编号
            /// </summary>
            public int CottagesId { get; set; }//栋舍编号
            /// <summary>
            /// 栏位编号
            /// </summary>
            public int BirsdayName { get; set; }//栏位编号
            /// <summary>
            /// 转种日期
            /// </summary>
            public DateTime TransplainData { get; set; }//转种日期
            /// <summary>
            /// 重量
            /// </summary>
            public decimal FemaleBeginWeight { get; set; }//重量
            /// <summary>
            /// 所在羊场
            /// </summary>
            public int FarmId { get; set; }
            /// <summary>
            /// 栋舍类型
            /// </summary>
            public int CottageTypeId { get; set; }
            /// <summary>
            /// 栋舍名称
            /// </summary>
            public string CottageName { get; set; }
            /// <summary>
            /// 负责人
            /// </summary>
            public int PersId { get; set; }
            /// <summary>
            /// 生产人员
            /// </summary>
            public int Production { get; set; }
            /// <summary>
            /// 状态
            /// </summary>
            public bool Status { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            [StringLength(100)]
            public string? Remark { get; set; }
            /// <summary>
            /// 主键id
            /// </summary>
            public int FieIdId { get; set; }
            /// <summary>
            /// 栏位名称
            /// </summary>
            [StringLength(30)]
            public string? FieIdName { get; set; }
            /// <summary>
            /// 所在栋舍编号
            /// </summary>
            public int CottageId { get; set; }
            /// <summary>
            /// 在场状态 0-待入场 1-在场
            /// </summary>
            public int StayStatus { get; set; }
        }
    }
}
